package com.huike.clues.service.impl;

import com.huike.clues.domain.SysRoleDept;
import com.huike.clues.domain.SysRoleMenu;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysRoleDeptMapper;
import com.huike.clues.service.ISysDeptService;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.exception.BaseException;
import org.apache.commons.collections4.list.TreeList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysDeptServiceImpl implements ISysDeptService {
    @Autowired
    private SysDeptMapper sysDeptMapper;
@Autowired
private SysRoleDeptMapper sysRoleDeptMapper;
    @Override
    public void deleteById(Long deptId) {
        sysRoleDeptMapper.deleteRoleDeptByDeptId(deptId);
        Integer row = sysDeptMapper.deleteById(deptId);
        if (row == 0) {
            throw new BaseException("删除失败");
        }
    }

    @Override
    public List<Integer> findRoleDept(Long roleId) {

        List<Integer> list=sysDeptMapper.findDeptIds(roleId);
        return list;
    }

    @Override
    public List<TreeSelect> findTreeDept() {
        ArrayList<TreeSelect> treeSelects = new ArrayList<>();
        List<SysDept> list = sysDeptMapper.list(null,null);
        HashMap<Long, TreeSelect> longTreeSelectHashMap = new HashMap<>();
        for (SysDept sysDept : list) {
            TreeSelect treeSelect = new TreeSelect();
            treeSelect.setId(sysDept.getDeptId());
            treeSelect.setLabel(sysDept.getDeptName());
            treeSelect.setSort(sysDept.getOrderNum());
            longTreeSelectHashMap.put(Long.valueOf(sysDept.getDeptId()), treeSelect);
            TreeSelect treeSelect1 = longTreeSelectHashMap.get(Long.valueOf(sysDept.getParentId()));
            if (treeSelect1 == null) {
                treeSelects.add(treeSelect);
            } else {
                treeSelect1.getChildren().add(treeSelect);
                Collections.sort(treeSelect1.getChildren(),(o1, o2) -> Integer.valueOf(o2.getSort())-Integer.valueOf(o1.getSort()));
            }
        }
        Collections.sort(treeSelects,(o1, o2) -> Integer.valueOf(o2.getSort())-Integer.valueOf(o1.getSort()));
        return treeSelects;
    }

    @Override
    public SysDept findById(Long deptId) {
        SysDept sysDept = sysDeptMapper.findById(deptId);
        if (sysDept == null) {
            throw new BaseException("回显失败");
        }
        return sysDept;
    }

    @Override
    public void update(SysDept sysDept) {
        String ancestors = sysDeptMapper.findancestors(sysDept.getParentId());
        ancestors += "," + sysDept.getParentId();
        sysDept.setAncestors(ancestors);
        sysDeptMapper.update(sysDept);
    }

    @Override
    public void save(SysDept sysDept) {
        String ancestors = sysDeptMapper.findancestors(sysDept.getParentId());
        ancestors += "," + sysDept.getParentId();
        sysDept.setAncestors(ancestors);
        Integer row = null;
        try {
            row = sysDeptMapper.save(sysDept);
        } catch (Exception e) {
            throw new BaseException(sysDept.getDeptName()+"重复,请重新添加");
        }
    }
}
